package com.xzq.socket.aio;

import com.xzq.socket.aio.server.AioServer;

import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;

/**
 * @Author xzq
 * @Description //TODO
 * @Date 2021/11/18 10:04
 * @Version 1.0.0
 **/
public abstract class ChannelInitializer implements CompletionHandler<AsynchronousSocketChannel, AioServer> {
    @Override
    public void completed(AsynchronousSocketChannel channel, AioServer attachment) {
        try {
            initChannel(channel);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            attachment.getServerSocketChannel().accept(attachment, this);   //再次接受客户端连接
        }
    }

    protected abstract void initChannel(AsynchronousSocketChannel channel) throws Exception;


    @Override
    public void failed(Throwable exc, AioServer attachment) {
        exc.getStackTrace();
    }
}
